package com.spring.springmvc.请求.传参;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

@RequestMapping("/session")
@RestController
public class 获取session {

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        //“name"是名称，不是值，后面的才是值
        session.setAttribute("name","zhangsan");
        return "session设置成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        return "session ="+name;
    }

    @RequestMapping("/getSession1")
    public String getSession1(@SessionAttribute("name") String name){
        return "name:"+name;
    }
}

